<template>
  <div class="system-user-dialog-container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.title" width="769px">
      <el-form ref="dictDialogFormRef" :model="state.ruleForm" :rules="rules" label-width="90px" size="default">
        <el-form-item label="字典类型">
          <el-input v-model="state.ruleForm.dictType" :disabled="true"/>
        </el-form-item>
        <el-form-item label="数据标签" prop="dictLabel">
          <el-input v-model="state.ruleForm.dictLabel" :disabled="formReadonly" placeholder="请输入数据标签"/>
        </el-form-item>
        <el-form-item label="数据键值" prop="dictValue">
          <el-input v-model="state.ruleForm.dictValue" :disabled="formReadonly" placeholder="请输入数据键值"/>
        </el-form-item>
        <el-form-item label="样式属性" prop="cssClass">
          <el-input v-model="state.ruleForm.cssClass" :disabled="formReadonly" placeholder="请输入样式属性"/>
        </el-form-item>
        <el-form-item label="显示排序" prop="dictSort">
          <el-input-number v-model="state.ruleForm.dictSort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="回显样式" prop="listClass">
          <el-select v-model="state.ruleForm.listClass">
            <el-option
                v-for="item in state.listClassOptions"
                :key="item.value"
                :label="item.label + '(' + item.value + ')'"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字典状态" prop="status">
          <el-radio-group v-model="state.ruleForm.status">
            <el-radio
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="state.ruleForm.remark" placeholder="请输入内容" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button size="default" @click="onCancel">取 消</el-button>
					<el-button size="default" type="primary" @click="onSubmit(dictDialogFormRef)">
						{{ state.dialog.submitTxt }}
					</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="dictDataDialog" setup>
import {computed, getCurrentInstance, nextTick, onMounted, reactive, ref} from 'vue';
import type {FormInstance, FormRules} from 'element-plus';
import {ElMessage} from 'element-plus';
import type {RoleType} from '/@/api/sys/types';
import {dictDataApi} from "/@/api/sys/dictData";
import {dictApi} from "/@/api/sys/dict";


// 獲取父組件的值
const props = defineProps({
  dictId: Number,
})

const {proxy} = getCurrentInstance();
const {sys_normal_disable} = proxy.useDict("sys_normal_disable");
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const dictDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    id: '',
    dictId: 0,
    dictType: '',
    dictCode: '',
    dictLabel: '',
    dictValue: '',
    cssClass: '',
    listClass: 'default',
    dictSort: 0,
    status: "1",
    remark: ''
  },
  deptData: [] as DeptTreeType[], // 部门数据
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: ''
  },
  roleList: [] as RoleType[],
  // 数据标签回显样式
  listClassOptions: [
    {
      value: "default",
      label: "默认"
    },
    {
      value: "primary",
      label: "主要"
    },
    {
      value: "success",
      label: "成功"
    },
    {
      value: "info",
      label: "信息"
    },
    {
      value: "warning",
      label: "警告"
    },
    {
      value: "danger",
      label: "危险"
    }
  ],
});

const rules = reactive<FormRules>({
  dictLabel: [{required: true, message: '不能为空', trigger: 'blur'}],
  dictValue: [{required: true, message: '不能为空', trigger: 'blur'}],
});

// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
  state.dialog.type = type;
  state.dialog.isShowDialog = true;
  if (type === 'edit') {
    state.dialog.title = '修改字典';
    state.dialog.submitTxt = '修 改';
  } else if (type === 'add') {
    state.dialog.title = '新增字典';
    state.dialog.submitTxt = '新 增';
  }
  nextTick(() => {
    if (type === 'add') {
      state.ruleForm.id = '';
      // 清空表单，此项需加表单验证才能使用
      dictDialogFormRef.value.resetFields();
    } else {
      state.ruleForm = Object.assign(state.ruleForm, row);
    }
  });
};
// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      state.ruleForm.dictId = <number>props.dictId;
      if (state.dialog.type === 'add') {
        dictDataApi()
            .addRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '新增成功');
                closeDialog();
                emit('refresh');
              }
            });
      } else if (state.dialog.type === 'edit') {
        dictDataApi()
            .updateRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '修改成功');
                closeDialog();
                emit('refresh');
              }
            });
      }
    }
  });
};

// 页面加载时
onMounted(() => {
  getType(<number>props.dictId);
});

const getType = (dictId: number) => {
  dictApi()
      .getDictInfo(dictId)
      .then(res => {
        state.ruleForm.dictType = res.data.dictType;
      })
};

const formReadonly = computed(() => {
  return state.dialog.type !== 'add' && state.dialog.type !== 'edit'
})
// 暴露变量
defineExpose({
  openDialog,
});
</script>
